package com.bawei.wyg.networkmodel.retrofit;


import com.bawei.wyg.commonapplication.BaseApplication;
import com.bawei.wyg.networkmodel.utils.BaseConstant;
import com.bawei.wyg.networkmodel.utils.X509Utils;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * final纺织被别人继承
 * 上传文件网络请求封装
 * */
public final class UpLoadRetrofit implements HttpImpl{
    //私有化构造器
    //想拿到对象只能通过建造者建造的对象获取
    private UpLoadRetrofit(){}
    //声明一个retrofit对象
    private static Retrofit retrofit;
    @Override
    public Retrofit getRetrofit() {
        return retrofit;
    }
    //添加一个静态内部类构造出reofit对象
    public static class Build{
        //构造器调用内部类中方法
        public Build(){
            createRetrofit();
        }
        //单独写出创建方法
        private void createRetrofit(){
            //log拦截器
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            //拦截器日志级别设置为BODY
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            // OkHttp是一个高效的HTTP客户端
            // 特性: 支持HTTP/2,允许所有同一个主机地址的请求共享同一个socket连接
            // 连接池减少请求延时 透明的GZIP压缩减少响应数据的大小
            //使用OkHttp下的建造者 建造对象
            OkHttpClient.Builder okbuilder = new OkHttpClient.Builder();
            InputStream inputStream = null;
            try {
                inputStream= BaseApplication.getAppContext().getAssets().open("tomcat.cer");
                X509TrustManager manager = X509Utils.createX509TrustManager(null);
                okbuilder.sslSocketFactory(X509Utils.createSSLSocket(manager),manager);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (CertificateException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnrecoverableKeyException e) {
                e.printStackTrace();
            } catch (KeyStoreException e) {
                e.printStackTrace();
            } catch (KeyManagementException e) {
                e.printStackTrace();
            }finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            okbuilder.hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            //添加拦截器
            okbuilder.addInterceptor(interceptor);
            //设置超时时间
            okbuilder.connectTimeout(5, TimeUnit.MINUTES);
            okbuilder.writeTimeout(5, TimeUnit.MINUTES);
            okbuilder.readTimeout(5, TimeUnit.MINUTES);

            //配置retrofit
            Retrofit.Builder builder = new Retrofit.Builder();
            //把OKhttp添加到client中建造出来
            builder.client(okbuilder.build());
            //添加url
            builder.baseUrl(BaseConstant.BaseUrl);
            builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
            //gson工厂类
            builder.addConverterFactory(GsonConverterFactory.create());
            //建造出来retrofit对象
            retrofit=builder.build();

        }
        //类型为build
        public UpLoadRetrofit build(){
            return new UpLoadRetrofit();
        }
    }
}
